package com.itheima.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.PmsPruductQueryParam;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.pojo.db.PmsProduct;
import com.itheima.pojo.db.Product;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductApiImpl implements PmsProductApi {
    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Override
    public void insert(PmsProduct product) {
        pmsProductMapper.insert(product);
    }

    @Override
    public IPage<PmsProduct> listByCondition(IPage<PmsProduct> page, PmsPruductQueryParam productQueryParam) {
        return pmsProductMapper.selectPage(page, new LambdaQueryWrapper<PmsProduct>().eq(PmsProduct::getDeleteStatus, 0)
                .eq(ObjectUtil.isNotEmpty(productQueryParam.getPublishStatus()), PmsProduct::getPublishStatus, productQueryParam.getPublishStatus())
                .eq(ObjectUtil.isNotEmpty(productQueryParam.getVerifyStatus()), PmsProduct::getVerifyStatus, productQueryParam.getVerifyStatus())
                .like(ObjectUtil.isNotEmpty(productQueryParam.getKeyword()), PmsProduct::getKeywords, productQueryParam.getKeyword())
                .eq(ObjectUtil.isNotEmpty(productQueryParam.getProductSn()), PmsProduct::getProductSn, productQueryParam.getProductSn())
                .eq(ObjectUtil.isNotEmpty(productQueryParam.getBrandId()), PmsProduct::getBrandId, productQueryParam.getBrandId())
                .eq(ObjectUtil.isNotEmpty(productQueryParam.getProductCategoryId()), PmsProduct::getProductCategoryId, productQueryParam.getProductCategoryId()));
    }

    @Override
    public List<PmsProduct> list(String keyword) {
        return pmsProductMapper.selectList(new LambdaQueryWrapper<PmsProduct>().eq(PmsProduct::getDeleteStatus, 0)
                .like(ObjectUtil.isNotEmpty(keyword), PmsProduct::getName, keyword)
                .or().eq(PmsProduct::getDeleteStatus, 0).like(ObjectUtil.isNotEmpty(keyword), PmsProduct::getProductSn, keyword));

    }

    @Override
    public int updateDeleteStatus(List<Long> ids, Integer deleteStatus) {
        PmsProduct product = new PmsProduct();
        product.setDeleteStatus(deleteStatus);
        List<PmsProduct> pmsProducts = pmsProductMapper.selectBatchIds(ids);
        for (PmsProduct pmsProduct : pmsProducts) {
            if (pmsProduct.getPublishStatus().equals(1)) {
                return 0;
            }
        }
        return pmsProductMapper.update(product, new LambdaUpdateWrapper<PmsProduct>().in(PmsProduct::getId, ids));
    }

    @Override
    public int updateNewStatus(List<Long> ids, Integer newStatus) {
        PmsProduct product = new PmsProduct();
        product.setNewStatus(newStatus);
        return pmsProductMapper.update(product, new LambdaUpdateWrapper<PmsProduct>().in(PmsProduct::getId, ids));
    }

    @Override
    public int updatePublishStatus(List<Long> ids, Integer publishStatus) {
        PmsProduct product = new PmsProduct();
        product.setPublishStatus(publishStatus);
        return pmsProductMapper.update(product, new LambdaUpdateWrapper<PmsProduct>().in(PmsProduct::getId, ids));
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        PmsProduct product = new PmsProduct();
        product.setRecommandStatus(recommendStatus);
        return pmsProductMapper.update(product, new LambdaUpdateWrapper<PmsProduct>().in(PmsProduct::getId, ids));
    }

    @Override
    public int updateVerifyStatus(List<Long> ids, Integer verifyStatus) {
        PmsProduct product = new PmsProduct();
        product.setVerifyStatus(verifyStatus);
        return pmsProductMapper.update(product, new LambdaUpdateWrapper<PmsProduct>().in(PmsProduct::getId, ids));
    }

    @Override
    public void updateById(PmsProduct product) {
        pmsProductMapper.updateById(product);
    }

    @Override
    public PmsProduct getUpdateInfo(Long id) {
        return pmsProductMapper.selectById(id);
    }

    @Override
    public void update(PmsProduct product, Long id) {
        pmsProductMapper.update(product, new LambdaUpdateWrapper<PmsProduct>().eq(PmsProduct::getProductCategoryId, id));
    }

    @Override
    public Product selectById(Long productId) {
        Product product = new Product();
        BeanUtils.copyProperties(pmsProductMapper.selectById(productId), product);
        return product;


    }
}
